<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Art;
use App\Cat;
use App\User;
use App\Comm;
use App\Dsp;
use Auth;

class ProController extends Controller
{
   public function index(){


   	//print_r($req);
   	
   	$user = DB::table('arts')->orderBy('pubtime')->paginate(6);
   	$ping = DB::table('comms')->orderBy('pubtime')->get();
    $name = DB::table('cats')->get();
   	//var_dump($user);
   	return view('index',['user'=>$user,'ping'=>$ping,'name'=>$name]);
   	
   	
   	//print_r($ping);
   }
   public function channel(){

    $pindao = DB::table('cats')->orderBy('cid')->get();
   	return view('channel',['pindao'=>$pindao]);

   }
   public function pro(Request $req,$id){
             //页面主区域的内容
            $art=Art::where('aid',$id)->first();
            $cat=Cat::where('cid',$art->cid)->first();
            $auth=User::where('uid',$art->uid)->first();

            //评论明细展示
            $comm=Comm::where('aid',$id)->orderBy('cmid','desc')->paginate(2);
            //评论数展示
            $dsp=Dsp::where('aid',$id)->get();
            //获取所有用户信息，得到评论中的用户头像和名字
            $users=User::get();
            //用户信息获取
            $user=$req->user()?$req->user():0;

    return view('pro',['art'=>$art,'cat'=>$cat,'user'=>$user,'auth'=>$auth,'comm'=>$comm,'dsp'=>$dsp,'users'=>$users]);
   }
   public function propost(Request $req){
      //评论详表的存储
         $comm=new Comm();
         $comm->aid=$req->aid;
         $comm->uid=$req->uid;
         $comm->content=$req->content;
         $comm->pubtime=time();
         $comm->save();
         //点赞，收藏，评论表的存储
         $dps=Dsp::where('aid',$req->aid)->first();
         if($dps){
            $dps->increment('pl',1);
         }else{
            $dsp=new Dsp();
            $dsp->aid=$req->aid;
            $dsp->uid=$req->uid;
            $dsp->pl=1;
            $dsp->save();
         }


         return redirect("/pro/$req->aid");

            
   }
   public function remen(){
    $re = DB::table('arts')->orderBy('like','desc')->paginate(6);
    $name = DB::table('cats')->get();
    $ping = DB::table('comms')->orderBy('pubtime')->get();
    //var_dump($re);
    return view('remen',['re'=>$re,'name'=>$name,'ping'=>$ping]);
   }
   public function jingxuan(){
    $jing = DB::table('arts')->orderBy('score','desc')->paginate(6);
    $name = DB::table('cats')->get();
    $ping = DB::table('comms')->orderBy('pubtime')->get();
    //var_dump($re);
    return view('jingxuan',['jing'=>$jing,'name'=>$name,'ping'=>$ping]);
   }
   public function pindao(){
    $pindao = DB::table('cats')->orderBy('cid','desc')->get();
    return view('pindao',['pindao'=>$pindao]);
   }
   public function album(){
   	$album = DB::table('albums')->orderBy('bid','desc')->paginate(6);
   	return view('album',['album'=>$album]);
   }
   public function idea($cid){
    
    
    $idea = DB::table('arts')
      ->join('cats','cats.cid','=','arts.cid')
      ->where('cats.cid',$cid)
      ->select('cats.catname','arts.*')
      ->orderBy('pubtime')
      ->paginate(6);
      
      $name = DB::table('cats')->get();

      $count = DB::table('arts')
      ->where('cid',$cid)
      ->count('aid');
      //var_dump($count);
      $aa[] = $count;
      return view('idea',['idea'=>$idea,'aa'=>$aa,'name'=>$name]);
      }
}